<?php
namespace app\index\model;
use think\Db;
use think\Model;

class Index_model {

	public function getindex() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		// $time = time();
		$time = date('Y-m-d H:i:s', time());
		// var_dump($time);die;
		//记录用户操作
		$prev = Db('_user_log')
			->where('user_id', $uid)
			->order('create_time desc')
			->limit(0, 1)
			->field('log_desc')
			->find();
		$prevdata = $prev['log_desc'];
		if ($prevdata !== '我的工作台') {
			//记录用户操作
			$data = [
				'user_id' => $uid,
				'log_action' => 'Index/index',
				'log_desc' => '我的工作台', //默认空
				'create_time' => $time,
				'is_delete' => 0, //默认0
			];
			$ires = db('_user_log')
				->insert($data);
		}

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		//获取用户历史记录
		$record = Db::table('cn_user_log')
			->alias('cul')
			->where('user_id', $uid)
			->field('log_desc')
			->order('create_time desc')
			->limit(9)
			->select();
		$second = Db::table('cn_user_log')
			->alias('cul')
			->where('user_id', $uid)
			->field('log_desc,create_time')
			->order('create_time desc')
			->limit(9)
			->select();
		$now = time();
		$s = [];
		for ($i = 0; $i < count($second); $i++) {
			$t = strtotime($second[$i]['create_time']);
			// var_dump($t);die;
			$o[$i]['create_time'] = ($now - $t);

			$d = floor($o[$i]['create_time'] / (3600 * 24));
			$h = floor(($o[$i]['create_time'] % (3600 * 24)) / 3600);
			$m = floor((($o[$i]['create_time'] % (3600 * 24)) % 3600) / 60);
			if ($d > '0') {
				$s[$i]['create_time'] = $d . '天' . $h . '小时' . $m . '分钟前';
			} else {
				if ($h != '0') {
					$s[$i]['create_time'] = $h . '小时' . $m . '分钟前';
				} else {
					$s[$i]['create_time'] = $m . '分钟前';
				}
			}

		}

		for ($j = 0; $j < count($record); $j++) {
			$amount[$j] = array_merge($record[$j], $s[$j]);
		}

		//获得最新消息
		//根据user_id查到对应的ord_id
		$org_id = Db::table('cn_user_organization_relation')
			->where('user_id', $uid)
			->field('org_id')
			->find();

		# code...
		$sql = "SELECT
                    cm.mail_title,
                    cm.mail_id,
                    cm.create_time
                FROM
                    cn_mail_addressee AS cma
                    LEFT JOIN cn_mail AS cm ON cma.mail_id = cm.mail_id
                WHERE
                    add_org_id = - 1
                    OR add_org_id = '" . $org_id['org_id'] . "'
                    AND mail_send_channel = 2
                    AND cma.is_delete != 1
                    AND cm.is_delete != 1
                ORDER BY
                    cm.update_time DESC
                    LIMIT 9";
		$mail = Db::query($sql);
		$file_num = Db::table('cn_interfile')
			->where('interfile_status', 3)
			->field('interfile_id')
			->select();
		$file_num = count($file_num);
		//借阅数量暂无
		$borrow_num = 0;
		$user_num = Db::table('cn_user')
			->where('is_delete', 0)
			->field('user_id')
			->select();
		$user_num = count($user_num);
		// dump($user_num);die;
		// var_dump($mail);die;
		// dump($mail);die;
		//获取文档数量 借阅数量 用户数
		$info = [
			'node' => $list,
			'amount' => $amount,
			'mail' => $mail,
			'id' => $uid,
			'file_num' => $file_num,
			'borrow_num' => $borrow_num,
			'user_num' => $user_num,
		];
		// dump($info);die;
		return $info;
	}

	// 根据登录id查询未读消息
	public function getnews() {
		$id = $_POST['id'];
		$res = Db::table('cn_mail')
			->alias('cm')
			->join('cn_mail_addressee cma', 'cma.mail_id = cm.mail_id')
			->where('cma.add_user_id', $id)
			->where('cm.is_delete', 'eq', 0)
			->where('cm.is_read', 'eq', 0)
			->where('cm.mail_send_channel', 'eq', 1)
			->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count')
			->group('cm.mail_id')
			->select();
		$data = json($res);
		return $data;
	}

}
?>